
/*
********************************************************************************
Description:		Import and clean SA302 enquiries dataset

Uses:				"$SA302indata/`taxyear'/`filename'`ext'"
					"$SA302outdata\SA302_variable_labels.csv"

Saves:				"$SA302outdata\SA302full_`year'.dta"

********************************************************************************
*/



***********************************
* cleanSA302: import all datasets *
***********************************

* Typical usage
	* cleanSA302, saving("${SA302outdata}\SA302_1999_2012.dta")
	
capture program drop cleanSA302
program define cleanSA302

	syntax, saving(string)
	
	import_sa302, years(1997(1)2012) original iv short_iv
	append_sa302, years(1997(1)2012) saving("`saving'")


end




********************************************************************************
* import_sa302: main program to import sa302 datasets and save to project file *
********************************************************************************

capture program drop import_sa302
program define import_sa302

	syntax, years(numlist) [original iv short_iv]

	set more off
	
	
	if "`original'"=="" & "`iv'"=="" & "`short_iv'"=="" {
		local original "original"
		local iv "iv"
		local short_iv "short_iv" 
	}
	
	* Original SA302 datasets
	*************************
	*************************
	* This section of the code deals with the original SA302 datasets including those calculated on VV
	* The section below deals with the new SA302 datasets calculated on IV
	
	if "`original'"=="original" {
	
		* 2011/12
		*********
		
		if  (`: list posof "2012" in years' > 0) {
		
			display as text "Importing 2012 (original)..."
		
			* Stata file already exists, so open it
			use "$SA302indata\2011-12\F2012.dta", clear

			* Tidy variables
			tidy_vars_sa302, year(2012) source(original)

			* Save file
			sort utr_no
			qui save "$SA302outdata\SA302full_2012.dta", replace

		}

		
		* 1996/97 to 2010/11
		********************

		* The things we need to have: year in filename, file extension, delimiter, anon_utr utr_no variable, tax_year variable

		* Loop backwards across years
		foreach year of numlist 2011(-1)1997 {
		
			* Only import year if requested
			if  (`: list posof "`year'" in years' > 0) {
			
				display as text "Importing `year' (original)..."
				
				* Construct tax year (for file path)
				local yr = substr("`year'",-2,2)
				local taxyear "`=`year'-1'-`yr'"
				
				* Construct filename
				local filename "F`year'"
				if (`year' == 2004) local filename "tax_calc`yr'_cut"
				else if inrange(`year',1999,2003) local filename "taxcalc`yr'_cut"
				else if inrange(`year',1997,1998) local filename "taxcalc`yr'"
				
				* Construct extension
				local ext ".txt"
				if (`year' <= 2004) local ext ".csv"
				
				* Delimiter
				local delimiter "|"
				if (`year' <= 2004) local delimiter ","
				
				* Import the file
				di `"qui import delimited using "$SA302indata/`taxyear'/`filename'`ext'", delimiters("`delimiter'") clear"'
				confirm file "$SA302indata/`taxyear'/`filename'`ext'"
				qui import delimited using "$SA302indata/`taxyear'/`filename'`ext'", delimiters("`delimiter'") clear

				* Tidy variables
				tidy_vars_sa302, year(`year') source(original)
				
				* Save file
				sort utr_no
				qui save "$SA302outdata\SA302full_`year'.dta", replace

			}
		}
		
	}
	
	
	* SA302 datasets calculated on IV
	*********************************
	*********************************
	
	* This includes all the IV returns (short and full) except in 2005, when it
	* seems the short return was stored in a different file (see next block of
	* code below)
	
	if "`iv'"=="iv" {
	
		* Loop backwards across years
		foreach year of numlist 2012(-1)1997 {
		
			* Only import year if requested
			if  (`: list posof "`year'" in years' > 0) {
	
				display as text "Importing `year' (IV)..."
				
				* Construct tax year (for file path)
				local yr = substr("`year'",-2,2)
				local taxyear "`=`year'-1'-`yr'"
				
				* Construct filename
				if (`year' < 2008) local filename "inv`yr'sa"
				else local filename "Sa302inv`yr'"
				
				* Construct extension
				local ext ".csv"
				
				* Delimiter
				local delimiter ","
				
				*Import full resturns of the tax calc on IV
				di as text `"import delimited using "$SA302IVindata/`filename'`ext'", delimiters("`delimiter'") clear"'
				qui import delimited using "$SA302IVindata/`filename'`ext'", delimiters("`delimiter'") clear
				
				* Tidy variables
				tidy_vars_sa302, year(`year') source(iv)
				
				* Save file
				sort utr_no
				qui save "$SA302outdata\SA302onIVfull_`year'.dta", replace

			}
		}
	}
	
	
	* SA302 datasets calculated on IV (short returns)
	*************************************************
	*************************************************
	
	* This now is only run for 2005 because that's the only year when short returns are in a separate file
	
	if "`short_iv'"=="short_iv" {
	
		* Loop backwards across years
		foreach year of numlist 2005(-1)2005 {
		
			* Only import year if requested
			if  (`: list posof "`year'" in years' > 0) {

				display as text "Importing `year' (IV short return)..."
				
				* Construct tax year (for file path)
				local yr = substr("`year'",-2,2)
				local taxyear "`=`year'-1'-`yr'"
				
				* Delimiter
				local delimiter ","
				
				*Import short returns of the tax calc on IV
				qui import delimited using "$SA302IVindata/short`yr'.csv", delimiters("`delimiter'") clear
				
				*the variable tax_year is missing in 2005 and we therefore generate it
				if (`year' == 2005) cap gen int tax_year = 2005 
				
				* Tidy variables
				tidy_vars_sa302, year(`year') source(short_iv)
				
				* Save file
				sort utr_no
				qui save "$SA302outdata\SA302onIVshort_`year'.dta", replace

			}
		}
	}


end



************************************************************
* tidy_vars_sa302: tidy up variables in open sa302 dataset *
************************************************************

capture program drop tidy_vars_sa302
program define tidy_vars_sa302

		syntax, year(integer) source(name)

		* Check source option contains a valid value
		assert inlist("`source'","original","iv","short_iv")
		
		* Make variables lower case
		qui rename _all, lower

		* Rename variables
		capture rename anon_utr utr_no
		capture rename utr_anon utr_no
		cap rename utref utr_no

		* Fill in any missing years 
		qui replace tax_year = `year' if (tax_year >= .)
		assert (tax_year == `year')

		* Label variables (post-2005 in original dataset since these are the only variables with sensible variable names)
		if ((`year' >= 2005) & ("`source'" == "original")) label_vars_sa302
		
		* Check identifiers
		sort utr_no
		isid utr_no
		
		* Compress
		quietly compress


end




***********************************************************
* label_vars_sa302: label variables in open sa302 dataset *
***********************************************************

capture program drop label_vars_sa302
program define label_vars_sa302

	
	* Open file of SA302 variable labels
	tempname labelsfh
	file open `labelsfh' using "$SA302outdata\SA302_variable_labels.csv", read text
	
	* Loop across lines of file labelling each variable
	file read `labelsfh' line
	while r(eof)==0 {
		gettoken varname varlabel : line, parse(",")
		local varname = lower("`varname'")
		local varlabel = substr(`"`varlabel'"',2,.)
		local varlabel `varlabel'
		capture label variable `varname' "`varlabel'"
		file read `labelsfh' line
	}
	
	* Close file of labels
	file close `labelsfh'

end






********************************************************
* append_sa302: append together all the SA302 datasets *
********************************************************

capture program drop append_sa302
program define append_sa302

	syntax, years(numlist) saving(string) 
	
	set more off


	label define sa302source 1 "Real SA302 (2005+)" 2 "Tax calculation on VV" 3 "Tax calculation on IV" 4 "Tax calculation on IV (2005 short return)"

	tempfile temp
	local firstyear = 1
	foreach year of local years {
		
		* Original SA302 datasets
		*************************
		*************************

		di "Opening original SA302 data (year: `year')"
		use "${SA302outdata}\SA302full_`year'.dta"

		
		if (`year' >= 2005) {
			more_tidy_vars_2005_onwards
			gen byte sa302source = 1
		}
		else if inrange(`year',1997,2004) {
			more_tidy_vars_`year'
			gen byte sa302source = 2
		}
		else {
			di as error "data not set up before 1997"
			exit 198
		}
		
		label values sa302source sa302source
		
		*Save the SA302 original dataset to be merged with the SA302onIV in a given year
		tempfile sa302orig
		qui save `sa302orig', replace
		

		* SA302 datasets calculated on IV
		*********************************
		*********************************

		di "Opening onIV data (year: `year')"
		use "${SA302outdata}\SA302onIVfull_`year'.dta"


		if (`year' >= 2008) {
			more_tidy_vars_onIV_2008_2012, year(`year')
		}
		else if inrange(`year',2005,2007) {
			more_tidy_vars_onIV_2005_2007, year(`year')
		}
		else if inrange(`year',1997,2004) {
			more_tidy_vars_`year'
		}
		else {
			di as error "data not set up before 1997"
			exit 198
		}

		
		gen byte sa302source = 3
		label values sa302source sa302source
		
		
		* SA302 datasets calculated on IV (short returns)
		*************************************************
		*************************************************
		
		* We only do this for year 2005 as the SA302onIVshort is already included in the SA302onIV for all other years than 2005
		
		if (`year'==2005) {
			
			
			*Save the SA302 on IVfull dataset
			tempfile sa302onIV
			qui save `sa302onIV', replace
		
			di "Opening onIVshort data (year: `year')"
			use "${SA302outdata}\SA302onIVshort_`year'.dta"
		
			*tidy IVshort
			more_tidy_vars_onIVshort_2005
			
			gen byte sa302source = 4
			label values sa302source sa302source
			
			*Append IVfull onto IVshort
			qui append using `sa302onIV'
			
			*Drop duplicates caused by short returns already being in SA302onIV but with missing observations
			bysort utr_no (sa302source): gen byte duplicate = (_N>1)
			drop if duplicate & (sa302source != 4)
			drop duplicate
			
		}
		
		* Append sa302orig onto IVfull (and IVshort if 2005)
		qui append using `sa302orig'
		
 		sort utr_no sa302source
		isid utr_no sa302source
		
		*Get rid of duplicates by deleting SA302 IV observations
		by utr_no (sa302source): gen int count = _N
		qui drop if ((count > 1) & (sa302source > 2))
		drop count
		
		*Sort and check for duplicates
		sort utr_no
		isid utr_no
		
		* Append years together
		if !`firstyear' quietly append using "`temp'"
		qui save `temp', replace
		local firstyear = 0
		
	}	
	
	
	*****************
	* SAVE THE DATA *
	*****************

	quietly compress
	sort utr_no tax_year 
	qui save "`saving'", replace

end


**********************************************************************************************
* more_tidy_vars_onIVshort_2005: create analysis variables for year 2005 using IV short returns   *
**********************************************************************************************

capture program drop more_tidy_vars_onIVshort_2005
program define more_tidy_vars_onIVshort_2005		
	
	*********************
	* Income components *
	*********************
	gen emp_directorship_inc=w1
	label var emp_directorship_inc "Employments and Directorships (PAYE), Q1"

	gen sharesch=0
	label var sharesch "Share Schemes, Q2"

	gen sempllinc=w1a
	label var sempllinc "Self-Employment (as a sole trader and Lloyds), Q3+Q3L"

	gen partnership_inc=0
	label var partnership_inc "Partnerships, Q4"

	gen propinc=w1b
	label var propinc "UK Land & Property, Q5"

	gen foreign_inc=0
	label var foreign_inc "Foreign Income, Q6"

	gen trust_estates=0
	label var trust_estates "Trusts and Estates, Q7"

	gen intinc=w21
	label var intinc "UK Interest, Q10"
	
	gen divs=w33
	label var divs "UK Dividends, Q10"
	
	gen intinc_divinc= intinc+divs
	label var intinc_divinc "Income from the sum of interests and dividents, Q10"
	
	gen pens=w1c
	label var pens "UK Pensions and benefits, Q11"

	gen gains_insurance_policies=0
	label var gains_insurance_policies "Gains on insurance policies, Q12"
	
	gen othinc=w1d
	label var othinc "Other Income, Q13"
	
	*****************
	* Income totals *
	*****************
	
	* Total income
	gen inc_tot_amt=w6+w21+w33
	label variable inc_tot_amt "Total income"

	* Total taxable income
	gen inc_taxable_amt=w8 + w23 + w35
	label variable inc_taxable_amt "Total taxable income"
	
	* Total taxable cg
	gen txble_cg=0
	label variable txble_cg "Total taxable cg"

	* Total taxable income and capital gain
	gen inc_cg_taxable_amt = inc_taxable_amt + txble_cg 
	label variable inc_cg_taxable_amt "Total taxable income and capital gain"
	
	**************
	* Tax totals *
	**************
	
	* Total IT, C4NI and student loan liability
	gen it_plus_cl4_amt=w79
	label variable it_plus_cl4_amt "Total IT, C4NI and student loan liability"
	
	* CG due
	gen cg_due=0
	
	* Total IT, C4NI, student loan and CGT liability (student loan is zero)
	gen it_cl4_cgt_amt = it_plus_cl4_amt + cg_due
	label variable it_cl4_cgt_amt "Total IT, C4NI, student loan and CGT liability"
	rename cg_due tot_cgt_amt
	label variable tot_cgt_amt "Total CGT"
	
	* TOTTAX_PAID_AMT
	gen tot_txpd=w82
	
	* TAX_ALREAD_REF_AMT
	gen tax_ref_=w85
	
	*UNPAID_TAX_BF_AMT
	gen unpd_tax=w77
	
	* Outstanding IT, C4NI, student loan and CGT liability
	gen tot_it_cl4_due_amt = it_plus_cl4_amt - tot_txpd
	gen tot_ic_cl4_due_amt = tot_it_cl4_due_amt + tot_cgt_amt
	gen tottax_cl4_due_amt = tot_ic_cl4_due_amt + tax_ref_ + unpd_tax
	rename tottax_cl4_due_amt tot_tax_due_amt
	label variable tot_tax_due_amt "Outstanding IT, C4NI, student loan and CGT liability"
	
	keep tax_year utr_no emp_directorship_inc sharesch sempllinc partnership_inc propinc /// 
	foreign_inc trust_estates intinc divs intinc_divinc pens gains_insurance_policies /// 
	othinc inc_tot_amt  inc_taxable_amt it_plus_cl4_amt it_cl4_cgt_amt tot_tax_due_amt /// 
	inc_cg_taxable_amt tot_cgt_amt
	
end




**********************************************************************************************
* more_tidy_vars_onIV_2008_2012: create analysis variables for years 2008 to 2012 using IV   *
**********************************************************************************************

capture program drop more_tidy_vars_onIV_2008_2012
program define more_tidy_vars_onIV_2008_2012

	syntax, year(integer)
		
	
	*********************
	* Income components *
	*********************
	if `year'==2008 gen emp_directorship_inc=c1_09+c1_34
	else if `year'>2008 gen emp_directorship_inc=c1_09+c1_37
	label var emp_directorship_inc "Employments and Directorships (PAYE), Q1"

	gen sharesch=c1_10
	label var sharesch "Share Schemes, Q2"

	gen sempllinc=c1_13+c1_14
	label var sempllinc "Self-Employment (as a sole trader and Lloyds), Q3+Q3L"

	gen partnership_inc=c1_17+c2_03+c3_01
	label var partnership_inc "Partnerships, Q4"

	gen propinc=c1_18
	label var propinc "UK Land & Property, Q5"

	if `year'==2008 gen foreign_inc=c1_22+c2_08+c3_05+c3_15
	else if (`year'==2009 | `year'==2010) gen foreign_inc=c1_24+c2_08+c3_15
	else if (`year'==2011 | `year'==2012) gen foreign_inc=c1_24+c2_08+c3_16
	label var foreign_inc "Foreign Income, Q6"

	if `year'==2008 gen trust_estates=c1_27+c2_12+c3_09+c3_20
	else if (`year'==2009 | `year'==2010) gen trust_estates=c1_30+c2_12+c3_09+c3_18+c3_19
	else if (`year'==2011 | `year'==2012) gen trust_estates=c1_30+c2_12+c3_10+c3_19+c3_20
	label var trust_estates "Trusts and Estates, Q7"

	gen intinc=c2_16
	label var intinc "UK Interest, Q10"
	
	if `year'<2011 gen divs=c3_13
	else if `year'>=2011 gen divs=c3_14
	label var divs "UK Dividends, Q10"
	
	gen intinc_divinc= intinc+divs
	label var intinc_divinc "Income from the sum of interests and dividents, Q10"
	
	if `year'==2008 gen pens=c1_28
	else if `year'>2008 gen pens=c1_31
	label var pens "UK Pensions and benefits, Q11"

	if `year'<2011 gen gains_insurance_policies=c2_17+c3_16
	else if `year'>=2011 gen gains_insurance_policies=c2_17+c3_17
	label var gains_insurance_policies "Gains on insurance policies, Q12"
	
	if `year'==2008 gen othinc=c1_32
	else if `year'>2008 gen othinc=c1_35
	label var othinc "Other Income, Q13"
	
	*****************
	* Income totals *
	*****************
	
	* Total income
	if (`year'==2008 | `year'==2011 | `year'==2012) gen inc_tot_amt=c3_21
	else if (`year'==2009 | `year'==2010) gen inc_tot_amt=c3_20
	label variable inc_tot_amt "Total income"

	* Total taxable income
	if `year'==2008 gen inc_taxable_amt=c5_28
	else if `year'>2008 gen inc_taxable_amt=c5_29
	label variable inc_taxable_amt "Total taxable income"
	
	* Total taxable cg
	if `year'==2008 gen txble_cg=c18_13
	else if (`year'==2009 | `year'==2010) gen txble_cg=c18_01
	else if (`year'==2011) gen txble_cg=c18_07+c18_14+c18_19
	else if `year'==2012 gen txble_cg=c18_07+c18_12
	label variable txble_cg "Total taxable cg"

	* Total taxable income and capital gain
	gen inc_cg_taxable_amt = inc_taxable_amt + txble_cg 
	label variable inc_cg_taxable_amt "Total taxable income and capital gain"
	
	**************
	* Tax totals *
	**************
	
	* Total IT, c4NI and student loan liability
	if `year'==2008 gen it_plus_cl4_amt=c12_08
	else if `year'>2008 gen it_plus_cl4_amt=c12_09
	label variable it_plus_cl4_amt "Total IT, C4NI and student loan liability"
	
	* CG due
	if `year'==2008 gen cg_due=c18_24+c12_13+c9_29
	else if (`year'==2009 | `year'==2010) gen cg_due=c18_05+c12_14+c9_29
	else if (`year'==2011) gen cg_due=c18_42+c12_14+c9_30
	else if `year'==2012 gen cg_due=c18_34+c12_14+c9_30
	
	* Total IT, C4NI, student loan and CGT liability
	gen it_cl4_cgt_amt = it_plus_cl4_amt + cg_due
	label variable it_cl4_cgt_amt "Total IT, C4NI, student loan and CGT liability"
	rename cg_due tot_cgt_amt
	label variable tot_cgt_amt "Total CGT"
	
	* TOTTAX_PAID_AMT
	if `year'==2008 gen tot_txpd=c12_11
	else if `year'>2008 gen tot_txpd=c12_12
	
	* TAX_ALREAD_REF_AMT
	if `year'==2008 gen tax_ref_=c12_15
	else if `year'>2008 gen tax_ref_=c12_16
	
	*UNPAID_TAX_BF_AMT
	if `year'==2008 gen unpd_tax=c12_16
	else if `year'>2008 gen unpd_tax=c12_17
	
	* Outstanding IT, C4NI, student loan and CGT liability
	gen tot_it_cl4_due_amt = it_plus_cl4_amt - tot_txpd
	gen tot_ic_cl4_due_amt = tot_it_cl4_due_amt + tot_cgt_amt
	gen tottax_cl4_due_amt = tot_ic_cl4_due_amt + tax_ref_ + unpd_tax
	rename tottax_cl4_due_amt tot_tax_due_amt
	label variable tot_tax_due_amt "Outstanding IT, C4NI, student loan and CGT liability"
	
	keep tax_year utr_no emp_directorship_inc sharesch sempllinc partnership_inc propinc /// 
	foreign_inc trust_estates intinc divs intinc_divinc pens gains_insurance_policies /// 
	othinc inc_tot_amt  inc_taxable_amt it_plus_cl4_amt it_cl4_cgt_amt tot_tax_due_amt /// 
	inc_cg_taxable_amt tot_cgt_amt
	
end


**********************************************************************************************
* more_tidy_vars_onIV_2005_2007: create analysis variables for years 2005 to 2007 using IV   *
**********************************************************************************************

capture program drop more_tidy_vars_onIV_2005_2007
program define more_tidy_vars_onIV_2005_2007

	syntax, year(integer)
	
	*********************
	* Income components *
	*********************
		
	gen emp_directorship_inc=c1_01+c1_02
	label var emp_directorship_inc "Employments and Directorships (PAYE), Q1"

	gen sharesch=c1_03
	label var sharesch "Share Schemes, Q2"

	gen sempllinc=c1_04+c1_05
	label var sempllinc "Self-Employment (as a sole trader and Lloyds), Q3+Q3L"

	gen partnership_inc=c1_06+c1_07+c1_08
	label var partnership_inc "Partnerships, Q4"

	gen propinc=c1_09
	label var propinc "UK Land & Property, Q5"

	gen foreign_inc=c1_10+c1_11+c1_12+c1_13
	label var foreign_inc "Foreign Income, Q6"

	gen trust_estates=c1_14+c1_15+c1_16+c1_17
	label var trust_estates "Trusts and Estates, Q7"

	gen intinc=c1_18
	label var intinc "UK Interest, Q10"
	
	gen divs=c1_19
	label var divs "UK Dividends, Q10"
	
	gen intinc_divinc= intinc+divs
	label var intinc_divinc "Income from the sum of interests and dividents, Q10"
	
	gen pens=c1_20
	label var pens "UK Pensions and benefits, Q11"

	gen gains_insurance_policies=c1_21+c1_22
	label var gains_insurance_policies "Gains on insurance policies, Q12"
	
	gen othinc=c1_23+c1_24
	label var othinc "Other Income, Q13"
	
	*****************
	* Income totals *
	*****************
	
	* Total income
	gen inc_tot_amt=c1_31
	label variable inc_tot_amt "Total income"

	* Total taxable income
	gen inc_taxable_amt=c3_28
	label variable inc_taxable_amt "Total taxable income"
	
	* Total taxable cg
	gen txble_cg=c14_13
	label variable txble_cg "Total taxable cg"

	* Total taxable income and capital gain
	gen inc_cg_taxable_amt = inc_taxable_amt + txble_cg 
	label variable inc_cg_taxable_amt "Total taxable income and capital gain"
	
	**************
	* Tax totals *
	**************
	
	* Total IT, C4NI and student loan liability
	gen it_plus_cl4_amt=c7_54
	label variable it_plus_cl4_amt "Total IT, C4NI and student loan liability"
	
	* CG due
	if `year'<2007 gen cg_due=c7_59 
	else if `year'==2007 gen cg_due=c7_61 
	
	* Total IT, C4NI, student loan and CGT liability
	gen it_cl4_cgt_amt = it_plus_cl4_amt + cg_due
	label variable it_cl4_cgt_amt "Total IT, C4NI, student loan and CGT liability"
	rename cg_due tot_cgt_amt
	label variable tot_cgt_amt "Total CGT"
	
	* TOTTAX_PAID_AMT
	if `year'<2007 gen tot_txpd=c7_57
	else if `year'==2007 gen tot_txpd=c7_59
	
	* Outstanding IT, C4NI, student loan and CGT liability
	gen tot_it_cl4_due_amt = it_plus_cl4_amt - tot_txpd
	gen tot_ic_cl4_due_amt = tot_it_cl4_due_amt + tot_cgt_amt
	gen tottax_cl4_due_amt = tot_ic_cl4_due_amt 
	rename tottax_cl4_due_amt tot_tax_due_amt
	label variable tot_tax_due_amt "Outstanding IT, C4NI, student loan and CGT liability"
	
	*TOT_IC_CL4_DUE_AMT
	if `year'<2007 gen tot_tax_due_amt_abs=c7_60
	else if `year'==2007 gen tot_tax_due_amt_abs=c7_62
	
	keep tax_year utr_no emp_directorship_inc sharesch sempllinc partnership_inc propinc /// 
	foreign_inc trust_estates intinc divs intinc_divinc pens gains_insurance_policies /// 
	othinc inc_tot_amt  inc_taxable_amt it_plus_cl4_amt it_cl4_cgt_amt tot_tax_due_amt /// 
	inc_cg_taxable_amt tot_cgt_amt
	
end


*********************************************************************************
* more_tidy_vars_2005_onwards: create analysis variables for years 2005 onwards *
*********************************************************************************

capture program drop more_tidy_vars_2005_onwards
program define more_tidy_vars_2005_onwards
	
	*********************
	* Income components *
	*********************

	rename emp_inc_ emp_directorship_inc
	label var emp_directorship_inc "Employments and Directorships (PAYE), Q1"

	rename shr_inc_ sharesch
	label var sharesch "Share Schemes, Q2"

	rename trll_inc sempllinc
	label var sempllinc "Self-Employment (as a sole trader and Lloyds), Q3+Q3L"

	rename ptr_inc_ partnership_inc
	label var partnership_inc "Partnerships, Q4"

	rename ifp_inc_ propinc
	label var propinc "UK Land & Property, Q5"

	rename for_inc_ foreign_inc
	label var foreign_inc "Foreign Income, Q6"

	rename tru_inc_ trust_estates
	label var trust_estates "Trusts and Estates, Q7"

	rename cas3_inc intinc
	label var intinc "UK Interest, Q10"
	
	rename ukdv_inc divs
	label var divs "UK Dividends, Q10"
	
	gen intinc_divinc= intinc+divs
	label var intinc_divinc "Income from the sum of interests and dividents, Q10"
	
	rename pens_inc pens
	label var pens "UK Pensions and benefits, Q11"

	rename ceg_inc_ gains_insurance_policies
	label var gains_insurance_policies "Gains on insurance policies, Q12"
	
	rename oth_inc_ othinc
	label var othinc "Other Income, Q13"
	
	*****************
	* Income totals *
	*****************
	
	* Total income
	rename tot_inc_ inc_tot_amt
	label variable inc_tot_amt "Total income"

	* Total taxable income
	rename tax_inc_ inc_taxable_amt
	label variable inc_taxable_amt "Total taxable income"

	* Total taxable income and capital gain
	gen inc_cg_taxable_amt = inc_taxable_amt + txble_cg 
	label variable inc_cg_taxable_amt "Total taxable income and capital gain"
	
	**************
	* Tax totals *
	**************
	
	* Total IT, C4NI and student loan liability
	rename itc4_amt it_plus_cl4_amt
	label variable it_plus_cl4_amt "Total IT, C4NI and student loan liability"

	* Total IT, C4NI, student loan and CGT liability
	gen it_cl4_cgt_amt = it_plus_cl4_amt + cg_due
	label variable it_cl4_cgt_amt "Total IT, C4NI, student loan and CGT liability"
	rename cg_due tot_cgt_amt
	label variable tot_cgt_amt "Total CGT"
	
	* Outstanding IT, C4NI, student loan and CGT liability
	gen tot_it_cl4_due_amt = it_plus_cl4_amt - tot_txpd
	gen tot_ic_cl4_due_amt = tot_it_cl4_due_amt + tot_cgt_amt
	gen tottax_cl4_due_amt = tot_ic_cl4_due_amt + tax_ref_ + unpd_tax
	rename tottax_cl4_due_amt tot_tax_due_amt
	label variable tot_tax_due_amt "Outstanding IT, C4NI, student loan and CGT liability"

	rename tot_itc4 tot_tax_due_amt_abs
	
	keep tax_year utr_no emp_directorship_inc sharesch sempllinc partnership_inc propinc /// 
	foreign_inc trust_estates intinc divs intinc_divinc pens gains_insurance_policies /// 
	othinc inc_tot_amt  inc_taxable_amt it_plus_cl4_amt it_cl4_cgt_amt tot_tax_due_amt /// 
	inc_cg_taxable_amt tot_cgt_amt
	
end


****************************************************************
* more_tidy_vars_2004: create analysis variables for year 2004 *
****************************************************************

capture program drop more_tidy_vars_2004
program define more_tidy_vars_2004

	rename c1_01 empinc
	rename c1_02 empinc_2

	gen emp_directorship_inc = empinc + empinc_2
	label var emp_directorship_inc "Employments and Directorships (PAYE), Q1"

	rename c1_03 sharesch
	label var sharesch "Share Schemes, Q2"

	rename c1_04 sempinc
	label var sempinc "Self-Employment (as a sole trader), Q3"

	rename c1_05 lloydsinc
	label var lloydsinc "Lloyds Underwriter, Q3L"
	
	gen sempllinc=sempinc+lloydsinc
	label var sempllinc "Self-Employment (as a sole trader and Lloyds), Q3+Q3L"
	
	rename c1_06 partinc_1
	label var partinc_1 "partnership employment inc"
	rename c1_07 partinc_2
	label var partinc_2 "partnership savings inc"
	rename c1_08 partinc_3
	label var partinc_3 "partnership dividend inc"

	gen partnership_inc = partinc_3 + partinc_2 + partinc_1
	label var partnership_inc "Partnerships, Q4"

	rename c1_09 propinc
	label var propinc "UK Land & Property, Q5"

	rename c1_10 forinc_1
	label var forinc_1 "foreign employment inc"
	rename c1_11 forinc_2
	label var forinc_2 "foreign lump sum inc"
	rename c1_12 forinc_3
	label var forinc_3 "foreign savings inc"
	rename c1_13 forinc_4
	label var forinc_4 "foreign dividend inc"
	rename c1_14 forinc_5

	gen foreign_inc = forinc_1+forinc_2+forinc_3+forinc_4+forinc_5
	label var foreign_inc "Foreign Income, Q6"

	rename c1_15 trusts_1
	label var trusts_1 "trust employment inc"
	rename c1_16 trusts_2
	label var trusts_2 "trust savings inc"
	rename c1_17 trusts_3
	label var trusts_3 "trust dividend inc"
	rename c1_18 trusts_4

	gen trust_estates = trusts_1+trusts_2+trusts_3+trusts_4
	label var trust_estates "Trusts and Estates,Q7"

	rename c1_19 intinc
	label var intinc "UK Interest,Q10"

	rename c1_20 divs
	label var divs "UK Dividends,Q10"
	
	gen intinc_divinc= intinc+divs
	label var intinc_divinc "Income from the sum of interests and dividents, Q10"
	
	rename c1_21 pens
	label var pens "UK Pensions and benefits,Q11"

	rename c1_22 gins_1
	rename c1_23 gins_2
	rename c1_24 gins_3

	gen gains_insurance_policies = gins_1+gins_2+gins_3
	label var gains_insurance_policies "Gains on insurance policies,Q12"

	rename c1_25 othinc
	label var othinc "Other Income,Q13"

	rename c1_32 inc_tot_amt
	label var inc_tot_amt "total income from all sources, including those with notional tax"

	rename c3_28 inc_taxable_amt
	label var inc_taxable_amt "Total Taxable Income"

	rename c14_13 tot_cg_taxable_amt
	label var tot_cg_taxable_amt "Total taxable CG"

	gen inc_cg_taxable_amt = inc_taxable_amt + tot_cg_taxable_amt
	label var inc_cg_taxable_amt "Total Taxable Income and CG"

	rename c7_52 it_plus_cl4_amt
	label var it_plus_cl4_amt "Income Tax and Class 4 NICs + Unpaid tax for earlier years + Student loans"

	rename c14_24 tot_cgt_amt
	label var tot_cgt_amt "Total CGT"

	gen it_cl4_cgt_amt = it_plus_cl4_amt + tot_cgt_amt
	label var it_cl4_cgt_amt "Income Tax and Class 4 NICs + Unpaid tax for earlier years + Student loans + CGT"

	rename c7_58 tot_tax_due_amt_net_c8_2_c8_3
	label var tot_tax_due_amt_net_c8_2_c8_3 "Total Tax and Class 4 NIC due (net of c8.2 and c8.3)"

	keep tax_year utr_no emp_directorship_inc sharesch sempllinc partnership_inc propinc foreign_inc trust_estates /// 
		 intinc divs pens intinc_divinc gains_insurance_policies othinc inc_tot_amt inc_taxable_amt it_plus_cl4_amt tot_cgt_amt it_cl4_cgt_amt ///
		 tot_tax_due_amt_net_c8_2_c8_3 inc_cg_taxable_amt intinc_divinc

end


****************************************************************
* more_tidy_vars_2003: create analysis variables for year 2003 *
****************************************************************

capture program drop more_tidy_vars_2003
program define more_tidy_vars_2003

	rename c1_01 empinc
	rename c1_02 empinc_2

	gen emp_directorship_inc = empinc + empinc_2
	label var emp_directorship_inc "Employments and Directorships (PAYE), Q1"

	rename c1_03 sharesch
	label var sharesch "Share Schemes, Q2"

	rename c1_04 sempinc
	label var sempinc "Self-Employment (as a sole trader), Q3"

	gen lloydsinc = c1_05 + c1_06
	label var lloydsinc "Lloyds Underwriter, Q3L"
	
	gen sempllinc=sempinc+lloydsinc
	label var sempllinc "Self-Employment (as a sole trader and Lloyds), Q3+Q3L"

	rename c1_07 partinc_1
	label var partinc_1 "partnership employment inc"
	rename c1_08 partinc_2
	label var partinc_2 "partnership savings inc"
	rename c1_09 partinc_3
	label var partinc_3 "partnership dividend inc"

	gen partnership_inc = partinc_3 + partinc_2 + partinc_1
	label var partnership_inc "Partnerships, Q4"

	rename c1_10 propinc
	label var propinc "UK Land & Property, Q5"

	rename c1_11 forinc_1
	label var forinc_1 "foreign employment inc"
	rename c1_12 forinc_2
	label var forinc_2 "foreign lump sum inc"
	rename c1_13 forinc_3
	label var forinc_3 "foreign savings inc"
	rename c1_14 forinc_4
	label var forinc_4 "foreign dividend inc"
	rename c1_15 forinc_5

	gen foreign_inc = forinc_1+forinc_2+forinc_3+forinc_4+forinc_5
	label var foreign_inc "Foreign Income, Q6"

	rename c1_16 trusts_1
	label var trusts_1 "trust employment inc"
	rename c1_17 trusts_2
	label var trusts_2 "trust savings inc"
	rename c1_18 trusts_3
	label var trusts_3 "trust dividend inc"
	rename c1_19 trusts_4

	gen trust_estates = trusts_1+trusts_2+trusts_3+trusts_4
	label var trust_estates "Trusts and Estates,Q7"

	rename c1_20 intinc
	label var intinc "UK Interest,Q10"

	rename c1_21 divs
	label var divs "UK Dividends,Q10"

	gen intinc_divinc= intinc+divs
	label var intinc_divinc "Income from the sum of interests and dividents, Q10"
	
	rename c1_22 pens
	label var pens "UK Pensions and benefits,Q11"

	rename c1_23 gins_1
	rename c1_24 gins_2
	rename c1_25 gins_3

	gen gains_insurance_policies = gins_1+gins_2+gins_3
	label var gains_insurance_policies "Gains on insurance policies,Q12"

	rename c1_26 othinc
	label var othinc "Other Income,Q13"

	rename c1_33 inc_tot_amt
	label var inc_tot_amt "total income from all sources, including those with notional tax"

	rename c3_28 inc_taxable_amt
	label var inc_taxable_amt "Total Taxable Income"

	rename c14_13 tot_cg_taxable_amt
	label var tot_cg_taxable_amt "Total taxable CG"

	gen inc_cg_taxable_amt = inc_taxable_amt + tot_cg_taxable_amt
	label var inc_cg_taxable_amt "Total Taxable Income and CG"

	rename c7_54 it_plus_cl4_amt
	label var it_plus_cl4_amt "Income Tax and Class 4 NICs + Unpaid tax for earlier years + Student loans"

	rename c14_24 tot_cgt_amt
	label var tot_cgt_amt "Total CGT"

	gen it_cl4_cgt_amt = it_plus_cl4_amt + tot_cgt_amt
	label var it_cl4_cgt_amt "Income Tax and Class 4 NICs + Unpaid tax for earlier years + Student loans + CGT"

	rename c7_60 tot_tax_due_amt_net_c8_2_c8_3
	label var tot_tax_due_amt_net_c8_2_c8_3 "Total Tax and Class 4 NIC due (net of c8.2 and c8.3)"

	keep tax_year utr_no emp_directorship_inc sharesch sempllinc partnership_inc propinc foreign_inc trust_estates /// 
		 intinc divs intinc_divinc pens gains_insurance_policies othinc inc_tot_amt inc_taxable_amt it_plus_cl4_amt tot_cgt_amt it_cl4_cgt_amt ///
		 tot_tax_due_amt_net_c8_2_c8_3 inc_cg_taxable_amt

end




****************************************************************
* more_tidy_vars_2002: create analysis variables for year 2002 *
****************************************************************

capture program drop more_tidy_vars_2002
program define more_tidy_vars_2002

	rename c1_01 empinc
	rename c1_02 empinc_2

	gen emp_directorship_inc = empinc + empinc_2
	label var emp_directorship_inc "Employments and Directorships (PAYE), Q1"

	rename c1_03 sharesch
	label var sharesch "Share Schemes, Q2"

	rename c1_04 sempinc
	label var sempinc "Self-Employment (as a sole trader), Q3"

	gen lloydsinc = c1_05 + c1_06
	label var lloydsinc "Lloyds Underwriter, Q3L"
	
	gen sempllinc=sempinc+lloydsinc
	label var sempllinc "Self-Employment (as a sole trader and Lloyds), Q3+Q3L"

	rename c1_07 partinc_1
	label var partinc_1 "partnership employment inc"
	rename c1_08 partinc_2
	label var partinc_2 "partnership savings inc"
	rename c1_09 partinc_3
	label var partinc_3 "partnership dividend inc"

	gen partnership_inc = partinc_3 + partinc_2 + partinc_1
	label var partnership_inc "Partnerships, Q4"

	rename c1_10 propinc
	label var propinc "UK Land & Property, Q5"

	rename c1_11 forinc_1
	label var forinc_1 "foreign employment inc"
	rename c1_12 forinc_2
	label var forinc_2 "foreign lump sum inc"
	rename c1_13 forinc_3
	label var forinc_3 "foreign savings inc"
	rename c1_14 forinc_4
	label var forinc_4 "foreign dividend inc"
	rename c1_15 forinc_5

	gen foreign_inc = forinc_1+forinc_2+forinc_3+forinc_4+forinc_5
	label var foreign_inc "Foreign Income, Q6"

	rename c1_16 trusts_1
	label var trusts_1 "trust employment inc"
	rename c1_17 trusts_2
	label var trusts_2 "trust savings inc"
	rename c1_18 trusts_3
	label var trusts_3 "trust dividend inc"
	rename c1_19 trusts_4

	gen trust_estates = trusts_1+trusts_2+trusts_3+trusts_4
	label var trust_estates "Trusts and Estates,Q7"

	rename c1_20 intinc
	label var intinc "UK Interest,Q10"

	rename c1_21 divs
	label var divs "UK Dividends,Q10"

	gen intinc_divinc= intinc+divs
	label var intinc_divinc "Income from the sum of interests and dividents, Q10"
	
	rename c1_22 pens
	label var pens "UK Pensions and benefits,Q11"

	rename c1_23 gins_1
	rename c1_24 gins_2
	rename c1_25 gins_3

	gen gains_insurance_policies = gins_1+gins_2+gins_3
	label var gains_insurance_policies "Gains on insurance policies,Q12"

	rename c1_26 othinc
	label var othinc "Other Income,Q13"

	rename c1_33 inc_tot_amt
	label var inc_tot_amt "total income from all sources, including those with notional tax"

	rename c3_28 inc_taxable_amt
	label var inc_taxable_amt "Total Taxable Income"

	rename c14_13 tot_cg_taxable_amt
	label var tot_cg_taxable_amt "Total taxable CG"

	gen inc_cg_taxable_amt = inc_taxable_amt + tot_cg_taxable_amt
	label var inc_cg_taxable_amt "Total Taxable Income and CG"

	rename c7_52 it_plus_cl4_amt
	label var it_plus_cl4_amt "Income Tax and Class 4 NICs + Unpaid tax for earlier years + Student loans"

	rename c14_24 tot_cgt_amt
	label var tot_cgt_amt "Total CGT"

	gen it_cl4_cgt_amt = it_plus_cl4_amt + tot_cgt_amt
	label var it_cl4_cgt_amt "Income Tax and Class 4 NICs + Unpaid tax for earlier years + Student loans + CGT"

	rename c7_58 tot_tax_due_amt_net_c8_2_c8_3 
	label var tot_tax_due_amt_net_c8_2_c8_3 "Total Tax and Class 4 NIC due (net of c8.2 and c8.3)"

	keep tax_year utr_no emp_directorship_inc sharesch sempllinc partnership_inc propinc foreign_inc trust_estates /// 
		 intinc divs intinc_divinc pens gains_insurance_policies othinc inc_tot_amt inc_taxable_amt it_plus_cl4_amt tot_cgt_amt  ///
		 it_cl4_cgt_amt tot_tax_due_amt_net_c8_2_c8_3 inc_cg_taxable_amt  ///

end



****************************************************************
* more_tidy_vars_2001: create analysis variables for year 2001 *
****************************************************************

capture program drop more_tidy_vars_2001
program define more_tidy_vars_2001

	rename c1_01 empinc
	rename c1_02 empinc_2

	gen emp_directorship_inc = empinc + empinc_2
	label var emp_directorship_inc "Employments and Directorships (PAYE), Q1"

	rename c1_04 sharesch
	label var sharesch "Share Schemes, Q2"

	rename c1_06 sempinc
	label var sempinc "Self-Employment (as a sole trader), Q3"

	gen lloydsinc = c1_08 + c1_09
	label var lloydsinc "Lloyds Underwriter, Q3L"
	
	gen sempllinc=sempinc+lloydsinc
	label var sempllinc "Self-Employment (as a sole trader and Lloyds), Q3+Q3L"

	rename c1_11 partinc_1
	label var partinc_1 "partnership employment inc"
	rename c1_12 partinc_2
	label var partinc_2 "partnership savings inc"
	rename c1_13 partinc_3
	label var partinc_3 "partnership dividend inc"

	gen partnership_inc = partinc_3 + partinc_2 + partinc_1
	label var partnership_inc "Partnerships, Q4"

	rename c1_15 propinc
	label var propinc "UK Land & Property, Q5"

	rename c1_17 forinc_1
	label var forinc_1 "foreign employment inc"
	rename c1_18 forinc_2
	label var forinc_2 "foreign lump sum inc"
	rename c1_19 forinc_3
	label var forinc_3 "foreign savings inc"
	rename c1_20 forinc_4
	label var forinc_4 "foreign dividend inc"
	rename c1_21 forinc_5

	gen foreign_inc = forinc_1+forinc_2+forinc_3+forinc_4+forinc_5
	label var foreign_inc "Foreign Income, Q6"

	rename c1_23 trusts_1
	label var trusts_1 "trust employment inc"
	rename c1_24 trusts_2
	label var trusts_2 "trust savings inc"
	rename c1_25 trusts_3
	label var trusts_3 "trust dividend inc"
	rename c1_26 trusts_4

	gen trust_estates = trusts_1+trusts_2+trusts_3+trusts_4
	label var trust_estates "Trusts and Estates,Q7"

	rename c1_28 intinc
	label var intinc "UK Interest,Q10"

	rename c1_30 divs
	label var divs "UK Dividends,Q10"

	gen intinc_divinc= intinc+divs
	label var intinc_divinc "Income from the sum of interests and dividents, Q10"
	
	rename c1_32 pens
	label var pens "UK Pensions and benefits,Q11"

	rename c1_34 gins_1
	rename c1_35 gins_2
	rename c1_36 gins_3

	gen gains_insurance_policies = gins_1+gins_2+gins_3
	label var gains_insurance_policies "Gains on insurance policies,Q12"

	rename c1_38 othinc
	label var othinc "Other Income,Q13"

	rename c1_46 inc_tot_amt
	label var inc_tot_amt "total income from all sources, including those with notional tax"

	rename c3_28 inc_taxable_amt
	label var inc_taxable_amt "Total Taxable Income"

	rename c14_01 tot_cg_taxable_amt
	label var tot_cg_taxable_amt "Total taxable CG"

	gen inc_cg_taxable_amt = inc_taxable_amt + tot_cg_taxable_amt
	label var inc_cg_taxable_amt "Total Taxable Income and CG"

	rename c7_57 it_plus_cl4_amt
	label var it_plus_cl4_amt "Income Tax and Class 4 NICs + Unpaid tax for earlier years + Student loans"

	rename c14_15 tot_cgt_amt
	label var tot_cgt_amt "Total CGT"

	gen it_cl4_cgt_amt = it_plus_cl4_amt + tot_cgt_amt
	label var it_cl4_cgt_amt "Income Tax and Class 4 NICs + Unpaid tax for earlier years + Student loans + CGT"

	rename c7_63 tot_tax_due_amt_net_c8_2_c8_3 
	label var tot_tax_due_amt_net_c8_2_c8_3 "Total Tax and Class 4 NIC due (net of c8.2 and c8.3)"

	keep tax_year utr_no emp_directorship_inc sharesch sempllinc partnership_inc propinc foreign_inc trust_estates /// 
		 intinc divs intinc_divinc pens gains_insurance_policies othinc inc_tot_amt inc_taxable_amt it_plus_cl4_amt tot_cgt_amt ///
		 it_cl4_cgt_amt tot_tax_due_amt_net_c8_2_c8_3 inc_cg_taxable_amt

end


****************************************************************
* more_tidy_vars_2000: create analysis variables for year 2000 *
****************************************************************

capture program drop more_tidy_vars_2000
program define more_tidy_vars_2000

	rename c1_13 empinc
	rename c1_15 empinc_2

	gen emp_directorship_inc = empinc + empinc_2
	label var emp_directorship_inc "Employments and Directorships (PAYE), Q1"

	rename c1_16 sharesch
	label var sharesch "Share Schemes, Q2"

	rename c1_19 sempinc
	label var sempinc "Self-Employment (as a sole trader), Q3"

	gen lloydsinc = c1_25
	label var lloydsinc "Lloyds Underwriter, Q3L"
	
	gen sempllinc=sempinc+lloydsinc
	label var sempllinc "Self-Employment (as a sole trader and Lloyds), Q3+Q3L"

	rename c1_31 partinc_1
	label var partinc_1 "partnership employment inc"
	rename c1_32 partinc_2
	label var partinc_2 "partnership savings inc"
	rename c1_33 partinc_3
	label var partinc_3 "partnership dividend inc"

	gen partnership_inc = partinc_3 + partinc_2 + partinc_1
	label var partnership_inc "Partnerships, Q4"

	rename c1_34 propinc
	label var propinc "UK Land & Property, Q5"

	rename c3_15 forinc_1
	label var forinc_1 "foreign employment inc"
	rename c3_16 forinc_2
	label var forinc_2 "foreign lump sum inc"
	rename c3_17 forinc_3
	label var forinc_3 "foreign savings inc"
	rename c3_18 forinc_4
	label var forinc_4 "foreign dividend inc"
	rename c3_19 forinc_5

	gen foreign_inc = forinc_1+forinc_2+forinc_3+forinc_4+forinc_5
	label var foreign_inc "Foreign Income, Q6"

	gen trust_estates = c3_26
	label var trust_estates "Trusts and Estates,Q7"

	rename c3_28 intinc
	label var intinc "UK Interest,Q10"

	rename c3_30 divs
	label var divs "UK Dividends,Q10"
	
	gen intinc_divinc= intinc+divs
	label var intinc_divinc "Income from the sum of interests and dividents, Q10"

	rename c3_32 pens
	label var pens "UK Pensions and benefits,Q11"
	
	gen gains_insurance_policies = c3_36
	label var gains_insurance_policies "Gains on insurance policies,Q12"

	rename c3_38 othinc
	label var othinc "Other Income,Q13"

	rename c3_46 inc_tot_amt
	label var inc_tot_amt "total income from all sources, including those with notional tax"

	rename c4_23 inc_taxable_amt
	label var inc_taxable_amt "Total Taxable Income"

	rename c9_02 tot_cg_taxable_amt
	label var tot_cg_taxable_amt "Total taxable CG"

	gen inc_cg_taxable_amt = inc_taxable_amt + tot_cg_taxable_amt
	label var inc_cg_taxable_amt "Total Taxable Income and CG"

	rename c11_62 it_plus_cl4_amt
	label var it_plus_cl4_amt "Income Tax + Class 4 NICs + Unpaid tax for earlier yrs(NO Studnt loans)"

	rename c9_17 tot_cgt_amt 
	label var tot_cgt_amt "Total CGT"

	gen it_cl4_cgt_amt = it_plus_cl4_amt + tot_cgt_amt
	label var it_cl4_cgt_amt "Income Tax + Class 4 NICs + Unpaid tax for earlier yrs + CGT(NO Student loans)"

	rename c12_04 tot_tax_due_amt 
	label var tot_tax_due_amt "Total Tax and Class 4 NIC due"

	keep tax_year utr_no emp_directorship_inc sharesch sempllinc partnership_inc propinc foreign_inc trust_estates /// 
		 intinc divs intinc_divinc pens gains_insurance_policies othinc inc_tot_amt inc_taxable_amt it_plus_cl4_amt tot_cgt_amt it_cl4_cgt_amt ///
		 tot_tax_due_amt inc_cg_taxable_amt
	 	 
end


****************************************************************
* more_tidy_vars_1999: create analysis variables for year 1999 *
****************************************************************

capture program drop more_tidy_vars_1999
program define more_tidy_vars_1999

	rename f1 emp_directorship_inc
	label var emp_directorship_inc "Employments and Directorships (PAYE), Q1"

	rename f2 sharesch
	label var sharesch "Share Schemes, Q2"

	rename f3 sempinc
	label var sempinc "Self-Employment (as a sole trader), Q3"
	
	gen sempllinc=sempinc
	label var sempllinc "Self-Employment (as a sole trader and Lloyds), Q3+Q3L"

	*Note that Lloyds income is not included in this aggregated variable in this year as it does not exist in the dataset

	rename f4 partnership_inc 
	label var partnership_inc "Partnerships, Q4"

	rename f5 propinc
	label var propinc "UK Land & Property, Q5"

	rename f6 foreign_inc 
	label var foreign_inc "Foreign Income, Q6"

	rename f7 trust_estates 
	label var trust_estates "Trusts and Estates,Q7"

	rename f8 intinc_divinc
	label var intinc_divinc "Income from the sum of interests and dividents, Q10"

	rename f9 pens
	label var pens "UK Pensions and benefits,Q11"

	rename f10 gains_insurance_policies
	label var gains_insurance_policies "Gains on insurance policies etc.,Q12"

	rename f11 othinc
	label var othinc "Other Income,Q13"

	rename f12 inc_tot_amt
	label var inc_tot_amt "total income from all sources, including those with notional tax"

	rename f21 inc_taxable_amt
	label var inc_taxable_amt "Total Taxable Income"

	rename f28a tot_cg_taxable_amt
	label var tot_cg_taxable_amt "Total taxable CG"
	 
	gen inc_cg_taxable_amt = inc_taxable_amt + tot_cg_taxable_amt
	label var inc_cg_taxable_amt "Total Taxable Income and CG"

	rename f74 it_plus_cl4_amt
	label var it_plus_cl4_amt "Income Tax + Class 4 NICs + Unpaid tax for earlier yrs (NO Student loans)"

	rename f60c tot_cgt_amt 
	label var tot_cgt_amt "Total CGT"

	gen it_cl4_cgt_amt = it_plus_cl4_amt + tot_cgt_amt
	label var it_cl4_cgt_amt "Income Tax + Class 4 NICs + Unpaid tax for earlier yrs + CGT(NO Student loans)"

	rename f78 tot_tax_due_amt
	label var tot_tax_due_amt "Total Tax and Class 4 NIC due"

	keep tax_year utr_no emp_directorship_inc sharesch sempllinc partnership_inc propinc foreign_inc trust_estates /// 
		 intinc_divinc pens gains_insurance_policies othinc inc_tot_amt inc_taxable_amt it_plus_cl4_amt tot_cgt_amt it_cl4_cgt_amt ///
		 tot_tax_due_amt inc_cg_taxable_amt
	qui compress

end


****************************************************************
* more_tidy_vars_1998: create analysis variables for year 1998 *
****************************************************************

capture program drop more_tidy_vars_1998
program define more_tidy_vars_1998

	rename f1 emp_directorship_inc
	label var emp_directorship_inc "Employments and Directorships (PAYE), Q1"

	rename f2 sharesch
	label var sharesch "Share Schemes, Q2"

	rename f3 sempinc
	label var sempinc "Self-Employment (as a sole trader), Q3"
	
	gen sempllinc=sempinc
	label var sempllinc "Self-Employment (as a sole trader and Lloyds), Q3+Q3L"

	*Note that Lloyds income is not included in this aggregated variable in this year as it does not exist in the dataset

	rename f4 partnership_inc 
	label var partnership_inc "Partnerships, Q4"

	rename f5 propinc
	label var propinc "UK Land & Property, Q5"

	rename f6 foreign_inc 
	label var foreign_inc "Foreign Income, Q6"

	rename f7 trust_estates 
	label var trust_estates "Trusts and Estates,Q7"

	rename f8 intinc_divinc
	label var intinc_divinc "Income from the sum of interests and dividents, Q10"

	rename f9 pens
	label var pens "UK Pensions and benefits,Q11"

	rename f10 gains_insurance_policies
	label var gains_insurance_policies "Gains on insurance policies etc.,Q12"

	rename f11 othinc
	label var othinc "Other Income,Q13"

	rename f12 inc_tot_amt
	label var inc_tot_amt "total income from all sources, including those with notional tax"

	rename f21 inc_taxable_amt
	label var inc_taxable_amt "Total Taxable Income"

	rename f28a tot_cg_taxable_amt
	label var tot_cg_taxable_amt "Total taxable CG"
	 
	gen inc_cg_taxable_amt = inc_taxable_amt + tot_cg_taxable_amt
	label var inc_cg_taxable_amt "Total Taxable Income and CG"

	rename f74 it_plus_cl4_amt
	label var it_plus_cl4_amt "Income Tax + Class 4 NICs + Unpaid tax for earlier yrs(NO Student loans)"

	rename f60c tot_cgt_amt 
	label var tot_cgt_amt "Total CGT"

	gen it_cl4_cgt_amt = it_plus_cl4_amt + tot_cgt_amt
	label var it_cl4_cgt_amt "Income Tax + Class 4 NICs + Unpaid tax for earlier yrs + CGT(NO Student loans)"

	rename f78 tot_tax_due_amt 
	label var tot_tax_due_amt "Total Tax and Class 4 NIC due"

	keep tax_year utr_no emp_directorship_inc sharesch sempllinc partnership_inc propinc foreign_inc trust_estates /// 
		 intinc_divinc pens gains_insurance_policies othinc inc_tot_amt inc_taxable_amt it_plus_cl4_amt tot_cgt_amt it_cl4_cgt_amt ///
		 tot_tax_due_amt inc_cg_taxable_amt
	qui compress

end


****************************************************************
* more_tidy_vars_1997: create analysis variables for year 1997 *
****************************************************************

capture program drop more_tidy_vars_1997
program define more_tidy_vars_1997

	rename f1 emp_directorship_inc
	label var emp_directorship_inc "Employments and Directorships (PAYE), Q1"

	rename f2 sharesch
	label var sharesch "Share Schemes, Q2"

	rename f3 sempinc
	label var sempinc "Self-Employment (as a sole trader), Q3"
	
	gen sempllinc=sempinc
	label var sempllinc "Self-Employment (as a sole trader and Lloyds), Q3+Q3L"

	*Note that Lloyds income is not included in this aggregated variable in this year as it does not exist in the dataset

	rename f4 partnership_inc 
	label var partnership_inc "Partnerships, Q4"

	rename f5 propinc
	label var propinc "UK Land & Property, Q5"

	rename f6 foreign_inc 
	label var foreign_inc "Foreign Income, Q6"

	rename f7 trust_estates 
	label var trust_estates "Trusts and Estates,Q7"

	rename f8 intinc_divinc
	label var intinc_divinc "Income from the sum of interests and dividents, Q10"

	rename f9 pens
	label var pens "UK Pensions and benefits,Q11"

	rename f10 gains_insurance_policies
	label var gains_insurance_policies "Gains on insurance policies etc.,Q12"

	rename f11 othinc
	label var othinc "Other Income,Q13"

	rename f12 inc_tot_amt
	label var inc_tot_amt "total income from all sources, including those with notional tax"

	rename f21 inc_taxable_amt
	label var inc_taxable_amt "Total Taxable Income"

	rename f28a tot_cg_taxable_amt
	label var tot_cg_taxable_amt "Total taxable CG"
	 
	gen inc_cg_taxable_amt = inc_taxable_amt + tot_cg_taxable_amt
	label var inc_cg_taxable_amt "Total Taxable Income and CG"

	rename f74 it_plus_cl4_amt
	label var it_plus_cl4_amt "Income Tax + Class 4 NICs + Unpaid tax for earlier yrs (NO Student loans)"

	rename f60c tot_cgt_amt 
	label var tot_cgt_amt "Total CGT"

	gen it_cl4_cgt_amt = it_plus_cl4_amt + tot_cgt_amt
	label var it_cl4_cgt_amt "Income Tax + Class 4 NICs + Unpaid tax for earlier yrs + CGT(NO Student loans)"

	rename f78 tot_tax_due_amt
	label var tot_tax_due_amt "Total Tax and Class 4 NIC due"

	keep tax_year utr_no emp_directorship_inc sharesch sempllinc partnership_inc propinc foreign_inc trust_estates /// 
		 intinc_divinc pens gains_insurance_policies othinc inc_tot_amt inc_taxable_amt it_plus_cl4_amt tot_cgt_amt it_cl4_cgt_amt ///
		 tot_tax_due_amt inc_cg_taxable_amt
	qui compress

end

